//
//  FileListModel.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/20.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//



//
#import "FileListModel.h"

@implementation FileListModel

- (void)getData:(NSString *)fromView listType:(NSString *)listType screenDic:(NSDictionary *)dic{
    switch ([fromView intValue]) {
        case 1:{
            [self getDataInformation:listType screenDic:dic]; //公文通知
        }
            break;
            
        case 2:{
            [self getDataMeet:listType screenDic:dic]; //会议管理
            
        }
            break;
        case 3:{
            [self getDataDocumentReview:listType screenDic:dic];  //公文批阅
        }
            break;
            
        case 4:{
            [self getDataWorkTask:listType screenDic:dic]; //工作任务
        }
            break;
        case 5:{
            [self getDataAddress:listType screenDic:dic]; //通讯录
        }
            break;
        default:{
            [XHToast showCenterWithText:@"选择界面超出范围"];
        }
            break;
    }
}
/**
 *  获取资讯列表
 *
 *  @param type 资讯列表类型：0-所有资讯 1-通知公告 2-企业新闻 .......
 */
- (void)getDataInformation:(NSString *)ListType screenDic:(NSDictionary *)screenDic{
    NSString *ashx = @"/Server/NewsHandlerNew.ashx";
    NSDictionary *dic = @{@"act":@"getl",@"ntype":ListType,@"keywords":screenDic[@"conditions"],@"dates":screenDic[@"starTime"],@"datee":screenDic[@"endTime"],@""@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"model:%@",model);
                NSMutableArray *mutableArray = [NSMutableArray new];
                
                for (NSDictionary *dic in model[@"data"]) {
                    if ([dic[@"nVS"] isEqualToString:@"0"]) {
                        [mutableArray insertObject:dic atIndex:0];
                    }else{
                        [mutableArray addObject:dic];
                    }
                }
                
                _dataArray = [mutableArray copy];
                self.block(YES);
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                self.block(NO);
            }
        }else{
            NSLog(@"错误%@",error);
            self.block(NO);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
    
    
}
/**
 *  会议管理列表
 *
 *  @param listType 会议列表类型
 */
- (void)getDataMeet:(NSString *)listType screenDic:(NSDictionary *)screenDic{
    NSString *ashx = @"/Server/MeetingHandler.ashx";
    NSString *verify = screenDic[@"verify"];

    switch ([verify intValue]) {
        case 0:{
            verify = @"join";
        }
            break;
        case 1:{
            verify = @"create";
        }
            break;
        case 2:{
            verify = @"all";
        }
            break;
        default:{
            verify = @"all";
        }
            break;
    }
    NSDictionary *dic = @{@"act":@"getm",@"verify":verify,@"keywords":screenDic[@"conditions"],@"Dates":screenDic[@"starTime"],@"Datee":screenDic[@"endTime"],@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    NSLog(@"%@",dic);
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSDate *nowDate = [NSDate date];
                NSLog(@"当前时间:%@",nowDate);
                NSLog(@"会议管理:%@",model);
                self.dataArray = model[@"data"];
                self.block(YES);
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                self.block(NO);
            }
        }else{
            NSLog(@"错误%@",error);
            self.block(NO);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
}

/**
 *  公文批阅
 *
 *  @param type 公文批阅类型：
 */
- (void)getDataDocumentReview:(NSString *)ListType screenDic:(NSDictionary *)screenDic{
    NSString *ashx = @"/Server/FlowsHandler.ashx";
    //判断在筛选时选择了什么分类：我的批阅 已经批阅 我的申请
    NSString *verify = screenDic[@"verify"];
    switch ([verify intValue]) {
        case 0:{
            verify = @"verify";
        }
            break;
        case 1:{
            verify = @"verified";
        }
            break;
        case 2:{
            verify = @"apply";
        }
            break;
        case 3:{
            verify = @"";
        }
            break;
        default:{
            verify = @"";
        }
            break;
    }
    
    NSDictionary *dic = @{@"act":@"getf",@"tid":ListType, @"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord],@"verify":verify,@"keywords":screenDic[@"conditions"]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                //将未批阅的移动到数组前方
                NSArray *Array = model[@"data"];
                NSMutableArray *mutableArray = [NSMutableArray new];
                for (NSDictionary *dic in Array) {
                    if ([dic[@"notDeal"] isEqualToString:@"1"]) {
                        [mutableArray insertObject:dic atIndex:0];
                    }else{
                        [mutableArray addObject:dic];
                    }
                }
                _dataArray = [mutableArray copy];
                NSLog(@"公文批阅请求数据:%@",_dataArray);
                self.block(YES);
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                self.block(NO);
            }
        }else{
            NSLog(@"错误%@",error);
            self.block(NO);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
    
    
}

/**
 *  工作任务
 *
 *  @param type 任务列表类型：
 */
- (void)getDataWorkTask:(NSString *)ListType screenDic:(NSDictionary *)screenDic{
    NSString *ashx = @"/Server/TaskHandler.ashx";;
    if ([ListType isEqualToString:@"-1"]) {
        ListType = @"";
    }
    
    //判断在筛选时选择了什么分类：我的批阅 已经批阅 我的申请
    NSString *verify = screenDic[@"verify"];
    switch ([verify intValue]) {
        case 0:{
            verify = @"exeute"; //我执行的
        }
            break;
        case 1:{
            verify = @"manage"; //我管理的
        }
            break;
        case 2:{
            verify = @"create"; //我创建的
        }
            break;
        case 3:{
            verify = @"all";    //所有
        }
            break;
        default:{
            verify = @"";
        }
            break;
    }

    NSDictionary *dic = @{@"act":@"getl",@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord],@"keywords":screenDic[@"conditions"],@"dates":screenDic[@"starTime"],@"datee":screenDic[@"endTime"],@"tid":ListType,@"verify":verify};
    NSLog(@"工作任务请求字典;%@",dic);
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"任务进程:%@",model);
                NSArray *array = model[@"data"];
                 NSMutableArray *mutableArray = [NSMutableArray new];
                for (int i = 0; i < 3; i++) {
                    for (NSDictionary *dic in array) {
                        if (i == 0) {
                            if ([dic[@"Status"] isEqualToString:@"0"]) {
                                [mutableArray addObject:dic];
                            }
                            
                        }else if (i == 1){
                            if ([dic[@"Status"] isEqualToString:@"-1"]) {
                                [mutableArray addObject:dic];
                            }
                        
                        }else{
                            if ([dic[@"Status"] isEqualToString:@"1"]) {
                                [mutableArray addObject:dic];
                            }
                        
                        }
                        
                    }
                }
                NSLog(@"可变数组  %@",mutableArray);
                _dataArray = [mutableArray copy];
                self.block(YES);
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                self.block(NO);
            }
        }else{
            NSLog(@"错误%@",error);
            self.block(NO);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
}


/**
 *  通讯录
 */
- (void)getDataAddress:(NSString *)ListType screenDic:(NSDictionary *)screenDic{
    NSString *ashx = @"/Server/ContactsBook.ashx";
    switch ([ListType intValue]) {
        case 0:{
            ListType = @"p";
        }
            break;
        case 1:{
            ListType = @"d";
        }
            break;
        default:{
            [XHToast showBottomWithText:@"目前只有两种类型"];
        }
            break;
    }
    NSDictionary *dic = @{@"type":ListType,@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord],@"keywords":screenDic[@"conditions"]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSLog(@"获取通讯录传入参数：%@",dic);
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"通讯录:%@",model);
                _dataArray = model[@"data"];
                [[NSUserDefaults standardUserDefaults] setObject:_dataArray forKey:@"ContactsBook"];
                self.block(YES);
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                self.block(NO);
            }
        }else{
            NSLog(@"错误%@",error);
 
            _dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"ContactsBook"];
            if (_dataArray.count > 0) {
                self.block(YES);
                [XHToast showBottomWithText:@"网络异常,将为您加载本地资源" duration:4.0f];
                return ;
            }
             [XHToast showBottomWithText:@"网络异常" duration:4.0f];
              self.block(NO);
        }
    }];
}



@end
